﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Sua_XoaTinTuc : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)// ko co phan nay ko update dc
        {
            LoadTinTuc();
        }
    }
    protected void LoadTinTuc()
    {
        dt = obj.loadDataTable("SELECT  * FROM [TinTucDienThoai] ORDER BY [MaBanTin] DESC");
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadTinTuc();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadTinTuc();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int mabantin = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        obj.XoaTinTuc(mabantin);
        Response.Write("<script language='javascript'> alert('Xoá thành công !' ) </script>");
        LoadTinTuc();
    }
    private void luuhinh(FileUpload a)// Luu Hinh
    {
        string strfilename;


        if (a.FileName != "")
        {
            strfilename = Server.MapPath("Upload\\" + a.FileName);
            a.PostedFile.SaveAs(strfilename);
        }
        else
        {



        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int mabantin = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        string tieude = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        string noidungtomtat= (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
        TextBox txtnoidung = GridView1.Rows[GridView1.EditIndex].FindControl("txtnoidung") as TextBox; ;
        string noidung = txtnoidung.Text;
        //string thongtin = (GridView3.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        FileUpload fileUpLoad = GridView1.Rows[GridView1.EditIndex].FindControl("FileUpload1") as FileUpload; ;// Tim fileupload trong edit item cua gridview
        string hinhanh = fileUpLoad.FileName.ToString();
        if (fileUpLoad.FileName != "")
        {
            luuhinh(fileUpLoad);//goi ham luu hinh add ten file upload vao
            obj.UpdateTinTuc(tieude, noidungtomtat, noidung, hinhanh, mabantin);
            GridView1.EditIndex = -1;
            LoadTinTuc();
        }
        else // Neu khong co hinh thi bo update hinh
        {
            obj.UpdateTinTucNeuHinhNull(tieude, noidungtomtat, noidung, mabantin);// goi ham update bo wa truong hinh
            GridView1.EditIndex = -1;
            LoadTinTuc();
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadTinTuc();
    }
   
}
